package com.neusoft.nep.grid.controller;



import com.neusoft.nep.grid.caluate.AlertUtils;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.grid.entity.LoginInfo;
import com.neusoft.nep.grid.entity.User;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;


public class loginController {
    @FXML
    private Button btn_Login;
    @FXML
    private TextField txt_logincode;
    @FXML
    private PasswordField txt_password;
    public void login() throws IOException {

        Map<String, User> userMap = FileUtils.readMapObject("Usergrid.txt");

        //判断用户存不存在
        String logincode = txt_logincode.getText();
        String password = txt_password.getText();
        if (userMap.containsKey(logincode)){
            User user = userMap.get(logincode);
            if(password.equals(user.getPassword())){
                AlertUtils.showDialog("网格员登录","登陆成功");
                LoginInfo.loginCode=logincode;
                FXMLLoader fxmlLoader = new FXMLLoader();
                URL url = getClass().getResource("../view/AQIfankui.fxml");
                fxmlLoader.setLocation(url);

                Parent root = fxmlLoader.load();
                Scene scene = btn_Login.getScene();
                scene.setRoot(root);
                Stage stage = (Stage) scene.getWindow();
                stage.show();
            }else{

                AlertUtils.showDialog("网格员登录","密码错误");
            }
        }else{
            AlertUtils.showDialog("网格员登录","网格员不存在");
        }
    }
}


